import math

with open('input.txt') as file:
    [start, buses] = file.read().splitlines()
    start = int(start)
    buses = [int(b) if b != 'x' else None for b in buses.split(',')]

    M = math.prod(b for b in buses if b is not None)
    result = 0

    for (i, b) in enumerate(buses):
        if b is None:
            continue
        
        Mi = M // b
        mi = pow(Mi, -1, b)
        result += (-i) * Mi * mi
    
    print(result % M)